<?php

namespace App\Http\Requests;

use App\Common\Status;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Contracts\Validation\Validator;
use Illuminate\Http\Exceptions\HttpResponseException;

class JobRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     * @return array
     */
    public function rules()
    {
        return [
            'id'                         => 'nullable|integer',
            'business_id'                => 'required|integer',
            'position_custom_name'       => 'required|string|max:60',
            'position_category_child_id' => 'required|integer',
            'work_exp'                   => 'required|integer',
            'education'                  => 'required|integer',
            'salary1'                    => 'required',
            'salary2'                    => 'required',
            'salary_month'               => 'nullable|integer',
            'welfare'                    => 'nullable|array',
            'city'                       => 'required|integer',
            'area'                       => 'required|integer',
            'email'                      => 'required|email',
            'release'                    => 'required|string',
            'release_job'                => 'required|string',
            'release_mobile'             => 'required|regex:/^1[3456789]\d{9}$/',
            'job_type'                   => 'required|integer',
            'describe'                   => 'required|string|max:800',
            'hot'                        => 'nullable|integer',
            'show'                       => 'nullable|integer',
        ];
    }

    public function messages(): array
    {
        return [
            'business_id.required'               => '企业必须',
            'position_custom_name.required'      => '职位自定义名称必须',
            'position_category_child_id.integer' => '职位子级分类必须',
            'work_exp.integer'                   => '工作经验必须',
            'education.integer'                  => '学历必须',
            'salary1.required'                   => '薪资必须',
            'salary2.required'                   => '薪资必须',
            'city.integer'                       => '城市必须',
            'area.integer'                       => '区县必须',
            'email.email'                        => 'email格式有误',
            'release.required'                   => '发布人必须',
            'release_job.required'               => '发布人职位必须',
            'release_mobile.regex'               => '发布人手机有误',
            'job_type.integer'                   => '招聘类型',
            'describe.max'                       => '岗位描述'
        ];
    }

    protected function failedValidation(Validator $validator)
    {
        $error = $validator->errors()->all();
        throw new HttpResponseException($this->fail(Status::ERROR, $error));
    }

    protected function fail(int $code, array $errors)
    {
        return response()->json(
            [
                'code'   => $code,
                'errors' => $errors,
            ]
        );
    }
}

